package com.examination.client.controller;

import com.examination.client.annotation.PassToken;
import com.examination.client.domian.dto.GetTokenDTO;
import com.examination.client.domian.dto.UserLoginDTO;
import com.examination.client.domian.dto.UserTokenDTO;
import com.examination.client.domian.vo.UserLoginVO;
import com.examination.client.entity.CourierSite;
import com.examination.client.entity.UserInfo;
import com.examination.client.enums.BusinessExceptionEnum;
import com.examination.client.service.ICourierSiteService;
import com.examination.client.service.IUserInfoService;
//import com.examination.client.util.JwtTokenUtil;
import com.examination.client.util.JwtTokenUtil;
import com.examination.client.util.MD5Util;
import com.examination.client.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * @Description:
 * @Auther: xiaodong
 * @Date: 2021/10/20
 */
@Slf4j
@RestController
@Api(tags = "登录")
@CrossOrigin(origins = "*")
public class LoginController {

    @Resource
    IUserInfoService userInfoService;

    @Resource
    ICourierSiteService courierSiteService;


    @PassToken
    @ApiOperation("登录")
    @PostMapping("/login")
    public Result<UserLoginVO> login(UserLoginDTO userLoginDTO) {

        UserInfo userInfo = userInfoService.findByUsername(userLoginDTO.getUserName(),userLoginDTO.getPlatformType());

        if(Objects.isNull(userInfo)){
            return Result.fail(BusinessExceptionEnum.USER_INVALID.code,BusinessExceptionEnum.USER_INVALID.msg);
        }
        if (!userLoginDTO.getPassword().equals(userInfo.getPassword())){
            return Result.fail(BusinessExceptionEnum.PASSWORD_WRONG.code,BusinessExceptionEnum.PASSWORD_WRONG.msg);
        }
        CourierSite courierSite = courierSiteService.getById(userInfo.getSiteId());

        //子账号不能登录主账号app
        if (userLoginDTO.getUserType() != null && userLoginDTO.getUserType() == 1 && courierSite.getParentId() > 0) {
            return Result.fail(BusinessExceptionEnum.LOGIN_ACCOUNT_ERROR.code,BusinessExceptionEnum.LOGIN_ACCOUNT_ERROR.msg);
        }
        UserTokenDTO userTokenDTO = new UserTokenDTO();
        BeanUtils.copyProperties(userInfo,userTokenDTO);
        userTokenDTO.setParentSiteId(courierSite.getParentId());

        String token = JwtTokenUtil.createToken(userTokenDTO);


        UserLoginVO userLoginVO = new UserLoginVO();
        BeanUtils.copyProperties(userInfo,userLoginVO);
        userLoginVO.setToken(token);
        return Result.success(userLoginVO);
    }

    @PassToken
    @ApiOperation("根据手机号登录")
    @PostMapping("/get/token")
    public Result<UserLoginVO> login(GetTokenDTO getTokenDTO) {
        UserInfo userInfo = userInfoService.findByUsername(getTokenDTO.getUserPhone(),getTokenDTO.getPlatformType());

        if(Objects.isNull(userInfo)){
            return Result.fail(BusinessExceptionEnum.USER_INVALID.code,BusinessExceptionEnum.USER_INVALID.msg);
        }
        CourierSite courierSite = courierSiteService.getById(userInfo.getSiteId());
        UserTokenDTO userTokenDTO = new UserTokenDTO();
        BeanUtils.copyProperties(userInfo,userTokenDTO);
        userTokenDTO.setParentSiteId(courierSite.getParentId());

        String token = JwtTokenUtil.createToken(userTokenDTO);


        UserLoginVO userLoginVO = new UserLoginVO();
        BeanUtils.copyProperties(userInfo,userLoginVO);
        userLoginVO.setToken(token);
        return Result.success(userLoginVO);
    }

    @PassToken
    @ApiOperation("退出登录")
    @PostMapping("/logout")
    public Result logout(HttpServletRequest request) {
        return Result.success();
    }

}
